﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WorldmapData
{
    public Color[] alphaColorArray = null;
    public int pixelCount;
    public int alphaMapWidth;
	public string diffuseTex = "";

    public void LoadData()
    {
        // 파일을 통해 데이터를 읽을 예정 일단 임의로.
        alphaMapWidth = 1280;
        pixelCount = 1280 * 720;
        alphaColorArray = new Color[pixelCount];

		diffuseTex = "blank";

//         for (int i = 0; i < (pixelCount / 4); ++i)
//         {
//             alphaColorArray[i] = Color.white;
//         }
    }
}

public static class WorldmapDataMgr 
{
    static Dictionary<int, PixelSplit> _dicPixelSplit = new Dictionary<int, PixelSplit>();
    static Dictionary<int, WorldmapData> _dicWorldMap = new Dictionary<int, WorldmapData>();

    static public void LoadWorld()
    {
        PixelSplit pixelSplit = new PixelSplit();
        pixelSplit.Create(1280);
        _dicPixelSplit.Add(1280, pixelSplit);

        //임의로 생성.

        WorldmapData worldMapData = new WorldmapData();
        worldMapData.LoadData();

        _dicWorldMap.Add(0,worldMapData);
    }

    public static Color[] GetPixelAlpha( int stage )
    {
        WorldmapData worldMapData = GetWorldmap(stage);

        if (worldMapData != null)
            return worldMapData.alphaColorArray;

        return null;

    }

    public static void SetPixelAlpha(int stage, Color[] alphaArray)
    {
        WorldmapData worldMapData = GetWorldmap(stage);

        if (worldMapData != null)
            worldMapData.alphaColorArray = alphaArray;
    }

    public static WorldmapData GetWorldmap(int stage)
    {
        WorldmapData worldMapData = null;

        if (_dicWorldMap.TryGetValue(stage, out worldMapData))
            return worldMapData;

        return null;
    }

    static PixelSplit GetPixelSplit(int size)
    {
        PixelSplit pixelSplit = null;
        if (_dicPixelSplit.TryGetValue(size, out pixelSplit))
            return pixelSplit;

        return null;
    }

    public static bool CollisionPixel(int stage, Vector2 center, float radius, ref Vector2 getPixel)
    {
        WorldmapData worldMapData = GetWorldmap(stage);
        if(worldMapData!=null)
        {
            PixelSplit pixelSplit = GetPixelSplit(worldMapData.alphaMapWidth);
            if (pixelSplit != null)
                return pixelSplit.CollisionPixel(center, radius, ref getPixel, ref worldMapData.alphaColorArray);
        }

        return false;
    }

    public static bool Collision2DCoord(int stage, Vector2 center, float radius, ref Vector2 getCoord)
    {
        Vector2 getPixel = Vector2.zero;
        WorldmapData worldMapData = GetWorldmap(stage);
        if (worldMapData != null)
        {
            PixelSplit pixelSplit = GetPixelSplit(worldMapData.alphaMapWidth);
            if (pixelSplit != null)
            {
                bool collPixel = pixelSplit.CollisionPixel(center, radius, ref getPixel, ref worldMapData.alphaColorArray);

                getCoord.x = getPixel.x - (pixelSplit._imgWidth / 2);
                getCoord.y = getPixel.y - (pixelSplit._imgHeight / 2);

                return collPixel;
            }
        }

        return false;
    }

    public static CollState GetPixelHeight(int stage, Vector2 pos, float radius, ref int height)
    {
        Vector2 getPos = Vector2.zero;
        WorldmapData worldMapData = GetWorldmap(stage);

        CollState getStatus = CollState.fail;

        if (worldMapData != null)
        {
            PixelSplit pixelSplit = GetPixelSplit(worldMapData.alphaMapWidth);
            if (pixelSplit != null)
            {
                getStatus = pixelSplit.GetPixelHeight(pos, radius, ref height, ref worldMapData.alphaColorArray);
                if (getStatus == CollState.success)
                {
                    getPos = GameUtils.PixelToPosition(new Vector2(0, (float)height), pixelSplit._imgWidth, pixelSplit._imgHeight);
                    height = (int)getPos.y;
                }
            }
        }
        return getStatus;
    }
}
